﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using SpringStudio.Common;
using SpringStudio.DAL;


namespace Sdepci.InfoCode.BLL.CodeDesign
{
    /// <summary>
    /// 详细信息编码管理类
    /// </summary>
    public class InfoManager
    {
        /// <summary>
        /// 新增详细信息码
        /// </summary>
        /// <param name="infoCode">信息编码</param>
        /// <param name="infoDesc">中文说明</param>
        /// <param name="ruleGuid">编码类型Guid</param>
        /// <param name="createUserId">创建用户证档号</param>
        /// <param name="serialReference">流水号参考码</param>
        /// <param name="serialNumber">当前流水号值</param>
        /// <returns></returns>
        public bool Add(string infoCode, string infoDesc, string ruleGuid, string createUserId, string serialReference, string serialNumber)
        {
            try
            {
                string sql = string.Empty;
                string infoGuid = Guid.NewGuid().ToString(); // 创建一个新的Guid
                sql = "Insert Into InfoCodeInfo (InfoGuid,InfoCode,InfoDesc,RuleGuid,CreateTime,CreateUserId,SerialReference,SerialNumber) Values(@InfoGuid,@InfoCode,@InfoDesc,@RuleGuid,@CreateTime,@CreateUserId,@SerialReference,@SerialNumber)";
                SqlHelper.ExecuteNonQuery(ConnectionString.Default, CommandType.Text, sql,
                    SqlHelper.NewSqlParameter("@InfoGuid", infoGuid),
                    SqlHelper.NewSqlParameter("@InfoCode", infoCode),
                    SqlHelper.NewSqlParameter("@InfoDesc", infoDesc),
                    SqlHelper.NewSqlParameter("@RuleGuid", ruleGuid),
                    SqlHelper.NewSqlParameter("@CreateTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")),
                    SqlHelper.NewSqlParameter("@CreateUserId", createUserId),
                    SqlHelper.NewSqlParameter("@SerialReference", serialReference),
                    SqlHelper.NewSqlParameter("@SerialNumber", serialNumber));
                return true;
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
                return false;
            }
        }

        /// <summary>
        /// 判断编码是否已存在（往往在用户保存操作超时的情况下发生）
        /// </summary>
        /// <param name="ruleGuid">编码类型Guid</param>
        /// <param name="infoCode">信息编码</param>
        /// <returns></returns>
        public bool Exist(string ruleGuid, string infoCode)
        {
            try
            {
                string sql = "Select InfoGuid From InfoCodeInfo Where RuleGuid=@RuleGuid And InfoCode=@InfoCode";
                DataSet ds = SqlHelper.ExecuteDataSet(ConnectionString.Default, CommandType.Text, sql,
                    SqlHelper.NewSqlParameter("@RuleGuid", ruleGuid),
                    SqlHelper.NewSqlParameter("@InfoCode", infoCode));
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
                return true;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="infoGuid">编码Guid</param>
        /// <returns>是否成功</returns>
        public bool Delete(string infoGuid)
        {
            try
            {
                string sql = string.Empty;
                sql = "Delete From InfoCodeInfo Where InfoGuid=@InfoGuid";
                SqlHelper.ExecuteNonQuery(ConnectionString.Default, CommandType.Text, sql,
                    SqlHelper.NewSqlParameter("@InfoGuid", infoGuid));

                return true;
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
                return false;
            }
        }

        /// <summary>
        /// 获取某类型编码的XML文档
        /// </summary>
        /// <param name="ruleGuid">编码类型Guid</param>
        /// <returns></returns>
        public XmlDocument GetInfoXmlDoc(string ruleGuid)
        {
            XmlDocument xmldoc;
            XmlNode xmlnode;
            XmlElement xmlroot;
            XmlElement xmlelement;

            xmldoc = new XmlDocument();
            xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
            xmldoc.AppendChild(xmlnode);

            xmlroot = xmldoc.CreateElement("root");
            xmldoc.AppendChild(xmlroot);

            try
            {
                string sql = "Select InfoGuid,InfoCode From InfoCodeInfo Where RuleGuid=@RuleGuid Order By CreateTime Desc";
                DataSet ds = SqlHelper.ExecuteDataSet(ConnectionString.Default, CommandType.Text, sql,
                    SqlHelper.NewSqlParameter("@RuleGuid", ruleGuid));
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        xmlelement = xmldoc.CreateElement("root_" + row["InfoGuid"].ToString());
                        xmlelement.SetAttribute("id", row["InfoGuid"].ToString());
                        xmlelement.SetAttribute("desc", row["InfoCode"].ToString());
                        xmlroot.AppendChild(xmlelement);
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
            }

            return xmldoc;
        }

        /// <summary>
        /// 获取某类型编码的XML文档（仅流水号）
        /// </summary>
        /// <param name="ruleGuid">编码类型Guid</param>
        /// <returns></returns>
        public XmlDocument GetExistXmlDoc(string ruleGuid)
        {
            XmlDocument xmldoc;
            XmlNode xmlnode;
            XmlElement xmlroot;
            XmlElement xmlelement;

            xmldoc = new XmlDocument();
            xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
            xmldoc.AppendChild(xmlnode);

            xmlroot = xmldoc.CreateElement("root");
            xmldoc.AppendChild(xmlroot);

            try
            {
                string sql = "Select Distinct SerialNumber From InfoCodeInfo Where RuleGuid=@RuleGuid Order By SerialNumber Desc";
                DataSet ds = SqlHelper.ExecuteDataSet(ConnectionString.Default, CommandType.Text, sql,
                    SqlHelper.NewSqlParameter("@RuleGuid", ruleGuid));
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        xmlelement = xmldoc.CreateElement("root_" + row["SerialNumber"].ToString());
                        xmlelement.SetAttribute("id", row["SerialNumber"].ToString());
                        xmlelement.SetAttribute("desc", row["SerialNumber"].ToString());
                        xmlroot.AppendChild(xmlelement);
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
            }

            return xmldoc;
        }
    }
}